//
//  MainViewController.swift
//  MyBlog
//
//  Created by User on 16/4/6.
//  Copyright © 2016年 personal. All rights reserved.
//

import UIKit

class MainViewController: UITabBarController {
    
    
    func clickComposeButton(){
    
    print("点我了")
    
    }
    override func viewDidLoad() {
        super.viewDidLoad()
        setUpControllers()
        setComposeButton()
    }
    override func viewWillAppear(animated: Bool) {
        super.viewWillAppear(animated)
        tabBar.bringSubviewToFront(composeButton)
    }
    lazy var composeButton:UIButton = UIButton(imageName: "tabbar_compose_icon_add", backImageName: "tabbar_compose_button")
}
// MARK: - 设置tabbar
extension MainViewController{
    func setComposeButton(){
        tabBar.addSubview(composeButton)
        let count = childViewControllers.count
        let w = tabBar.bounds.width / CGFloat(count) - 1
        composeButton.frame = CGRectInset(tabBar.bounds, 2 * w, 0)
        composeButton.addTarget(self, action: "clickComposeButton", forControlEvents:.TouchUpInside )
    }
    
    func setUpControllers(){
        //不要设置成bartintcolor，应该是tincolor
        // tabBar.barTintColor = UIColor.orangeColor()
//        tabBar.tintColor = UIColor.orangeColor()
        
        setUpOneController(HomeTableViewController(), title: "首页", imageName: "tabbar_home")
        setUpOneController(MessageTableViewController(), title: "消息", imageName: "tabbar_message_center")
        
        addChildViewController(UIViewController())
        setUpOneController(DiscoverTableViewController(), title: "发现", imageName: "tabbar_discover")
        setUpOneController(ProfileTableViewController(), title: "我", imageName: "tabbar_profile")
        
    }
    
    func setUpOneController(controller:UIViewController , title:String, imageName:String){
        
        
        controller.title = title
        controller.tabBarItem.image = UIImage(named: imageName)
        //以下代码设置选中图片无效,因为图片会被渲染
        //controller.tabBarItem.selectedImage = UIImage(named: imageName + "_heighlited")
        
        let nav = UINavigationController(rootViewController: controller)
        addChildViewController(nav)
        
    }
    
    
    
}